#!/usr/bin/python
# -*- coding: utf8 -*-


import argparse
import sys
import os
import time

reload(sys)
sys.setdefaultencoding('utf-8')


# ----- 开始时间

def __timestamp():
	return int(time.mktime(time.localtime(int(time.time()))))

__scriptStartTimestamp = __timestamp()


# ----- 根据项目修改的常量

from nat_lib.global_ import nat_global
from bs4 import BeautifulSoup

__projectname = nat_global.getStr("PROJECTNAME")
__APP_NAME = nat_global.getStr("APP_NAME")
__APP_LOGO_FILE = nat_global.getStr("APP_LOGO_FILE")
__PLATFORM = nat_global.getStr("PLATFORM")
__PROJECT_GIT_URL = nat_global.getStr("PROJECT_GIT_URL")
_TEAM_ID = nat_global.getStr("TEAM_ID")
_BUNDLE_ID = nat_global.getStr("BUNDLE_ID")
_IPA_FILE_SERVER = nat_global.getStr("IPA_FILE_SERVER")

_ITC_API_KEY = nat_global.getStr("ITC_API_KEY")
_ITC_ISSUER_ID = nat_global.getStr("ITC_ISSUER_ID")

_CERTIFICATE_INFO_ADHOC = nat_global.get("CERTIFICATE_INFO_ADHOC")   # 财商：正式
_EXTRA_ADHOC_BUNDLEID_PROVISION_LIST = nat_global.get("EXTRA_ADHOC_BUNDLEID_PROVISION_LIST")
_CERTIFICATE_INFO_APPSTORE = nat_global.get("CERTIFICATE_INFO_APPSTORE")   # 财商：正式
_EXTRA_APPSTORE_BUNDLEID_PROVISION_LIST = nat_global.get("EXTRA_APPSTORE_BUNDLEID_PROVISION_LIST")

__QIYE_WECHAT_WEBHOOK = nat_global.getStr("QIYE_WECHAT_WEBHOOK")

__QRCODE_BG_PNG_PATH = nat_global.getStr("QRCODE_BG_PNG_PATH")
__QRCODE_BG_STANDBY_PNG_PATH = nat_global.getStr("QRCODE_BG_STANDBY_PNG_PATH")
__GIT_BRANCH_MODE = nat_global.getStr("GIT_BRANCH_MODE")
__POD_UPDATE_FQ_RESOURCE = nat_global.getBool("POD_UPDATE_FQ_RESOURCE")

__APK_SCRIPT_PATH = nat_global.getStr("APK_SCRIPT_PATH")


# ----- 不常修改的常量
__PROJECT_DIR_PREFIX = "/Users/guoliang.zhu/Documents/MyWorks"

# _ACCOUNT_USER = "luxiaolong"
# _ACCOUNT_PASSWD = "654321qwerQ"

__SERVER_HOST_IP = "10.20.22.224"
_DOWNLOAD_IPA_URL_DOMAIN = "https://gitee.com/caimter/IPAS/raw/master/"
__IPADIR = ""

__DEFAULT_GITEE = "https://gitee.com/caimter/DownloadIPA.git"
__DEFAULT_GITEE_RAW = "https://gitee.com/caimter/DownloadIPA/raw/master"

_CODE_FILENAME = ".code"



import datetime

from nat_lib.archive import nat_path
from nat_lib.archive import nat_file
from nat_lib.archive import nat_plist
from nat_lib.log import nat_console
from nat_lib.var_type import nat_string
from nat_lib.system import nat_cmd
from nat_lib.vcs import nat_git
from nat_lib.media import nat_image
from nat_lib.var_type import nat_version
from nat_lib.thd.ios import nat_ipa
from nat_lib.thd.wechat import nat_group_robot
from nat_lib.thd.qrcode import nat_qrcode

from bs4 import BeautifulSoup
import qrcode
from PIL import Image



# ----- 解析命令行
_parser = argparse.ArgumentParser()


_group = _parser.add_mutually_exclusive_group(required=False)
_group.add_argument("-p", "--projectDirectory", help=u"项目目录", type=str)

_parser.add_argument("-v", "--version", help=u"迭代版本号，如：1.13", type=str, required=True)
_parser.add_argument("--vsn", help=u"版本号；如：1.0.0", type=str, required=False)
_parser.add_argument("--silence", help=u"不发消息到企业微信群", action="store_true")
_parser.add_argument("--fastlane", help=u"是否使用fastlane打包", action="store_true")


_groupMode = _parser.add_mutually_exclusive_group(required=False)
_groupMode.add_argument("--release", help=u"开启 Release 模式", action="store_true")
_groupMode.add_argument("--staging", help=u"开启 Staging 模式", action="store_true")
_groupMode.add_argument("--debug", help=u"开启 Debug 模式", action="store_true")
_groupMode.add_argument("--appstore", help=u"上传到 appstore", action="store_true")

_parser.add_argument("--releaseNote", help=u"更新日志", type=str)

# android 特有选项
_parser.add_argument("--args", help=u"android 参数列表；以空格为分隔符", type=str)
_parser.add_argument("--commitInfo", help=u"android 提交信息", type=str)


_args = _parser.parse_args()

_isFastlane = _args.fastlane
_version = nat_string.trim(_args.version)
_versionName = nat_string.trim(_args.vsn)
_projectDir = nat_path.getlastPath()

_isSilence = _args.silence

_isModeRelease = _args.release
_isModeStaging = _args.staging
_isModeAppStore = _args.appstore

_androidArgs = _args.args
_releaseNote = _args.releaseNote
if None == _releaseNote:
	_releaseNote = u""

_commitInfo = _args.commitInfo
if None == _commitInfo:
	commitInfo = u""


# =====自定义：
if _isModeAppStore:
	_mode = "appstore"
elif _isModeStaging:
	_mode = "staging"
elif _isModeRelease:
	_mode = "release"
else:
	_mode = "debug"


_projectName = os.path.basename(__PROJECT_GIT_URL).split(".")[0]
_platform = __PLATFORM
_isIOS = "ios" == __PLATFORM
__projectInfoPlistPath = "%s/%s/%s/Info.plist" %(_projectDir, _projectName, _projectName)

if _isIOS:
	if _isModeAppStore:
		_certificateName = _CERTIFICATE_INFO_APPSTORE[0]
		_provisioningName = _CERTIFICATE_INFO_APPSTORE[1]
	else:
		_certificateName = _CERTIFICATE_INFO_ADHOC[0]
		_provisioningName = _CERTIFICATE_INFO_ADHOC[1]


# ----- 内部函数
def __exit(content):
	nat_console.exit(u"%s ===" %(content))

def __warn(content):
	nat_console.warn(u"%s ===" %(content))
def __debug(content):
	nat_console.debug(u"%s ===" %(content))


# fastlane打包
def fastlaneDoArchive(appURL):
	# 当前目录
	pwd = nat_path.getCwd()
	_fromFastfile = pwd + "/Fastfile"
	# 当前目录的上级目录
	pwdLast = nat_path.getlastPath()
	_toFastfile = ""

	_fastlanePath = pwdLast + "/fastlane"
	if not nat_path.isExist(_fastlanePath):
		# 切换工作目录
		nat_path.changeCwd(pwdLast)
		# 创建fastlane
		nat_cmd.cmd("fastlane init")

		_toFastfile = nat_path.getCwd() + "/fastlane"
		if nat_cmd.cmd("cp -a -f %s %s" %(_fromFastfile, _toFastfile)):
			__warn(u"拷贝Fastfile文件成功啦")
		else:
			__exit(u"拷贝失败了，兄嘚！赶紧看看咋回事儿吧")
	else:
		# 进入fastlane所在目录
		nat_path.changeCwd(nat_path.getlastPath())
		_toFastfile = nat_path.getCwd() + "/fastlane"
		if nat_cmd.cmd("cp -a -f %s %s" %(_fromFastfile, _toFastfile)):
			__warn(u"拷贝Fastfile文件成功啦")
		else:
			__exit(u"拷贝失败了，兄嘚！赶紧看看咋回事儿吧")

		# 获取fastlane参数
		export_method = nat_global.get("export_method")
		configuration = _mode
		scheme = nat_global.get("scheme")
		version = _version
		output_directory = archive._getExportedArchiveDirBasename()
		# fastlane执行
		fastlaneExe = "fastlane package export_method:%s configuration:%s scheme:%s v:%s o:%s n:%s appurl:%s" %(export_method, configuration, scheme, version, output_directory, _projectName,appURL)
		__warn(u"开始执行%s" %(fastlaneExe))
		__warn(u"ipa下载地址：%s" %(appURL))
		return 0 == nat_cmd.cmd(fastlaneExe)

# 根据 issue code 获取标题
def _reqJiraTitle(code):
	jiraUrl = u"http://jira.xiaobangtouzi.com/browse/FQ-%i" %(code)

	cmdStr = u"curl -u %s:%s %s" %(_ACCOUNT_USER, _ACCOUNT_PASSWD, jiraUrl)
	html = os.popen(cmdStr).read()
	htmlLabelArr = BeautifulSoup(html, "html.parser").select("#summary-val")
	if None != htmlLabelArr and None != htmlLabelArr[0]:
		return htmlLabelArr[0].get_text()
	else:
		return u"FQ-%i" %(code)

# 解析更新内容行为 md 字符串
def _parseUpdateLines(contentLineArr):
	content = u""

	for contentLine in contentLineArr:
		contentLine = contentLine.strip()

		if "" == contentLine:
			continue

		# info 类别：任务
		elif contentLine.startswith("+"):
			try:
				jiraCode = int(contentLine)
				jiraIssueTitle = _reqJiraTitle(jiraCode)
				content += u"- **<font color=\"info\">[任务]</font>** [%s](http://jira.xiaobangtouzi.com/browse/FQ-%i)\n" %(jiraIssueTitle, jiraCode)
			except:
				contentLine = contentLine[1:].strip()
				content += u"- **<font color=\"info\">[任务]</font>** %s\n" %(contentLine)

		# warning 类别：bug
		elif contentLine.startswith("-"):
			contentLine = contentLine[1:].strip()
			try:
				jiraCode = int(contentLine)
				jiraIssueTitle = _reqJiraTitle(jiraCode)
				content += u"- **<font color=\"warning\">[bug]</font>** [%s](http://jira.xiaobangtouzi.com/browse/FQ-%i)\n" %(jiraIssueTitle, jiraCode)
			except:
				nat_console.exit(u"- 类别必须填写 jira 的单号数字，当前内容错误：%s" %(contentLine))

		# comment 类别：
		elif contentLine.startswith("*"):
			contentLine = contentLine[1:].strip()
			content += u"- **<font color=\"comment\">[备注]</font>** %s\n" %(contentLine)

		# 引用 类别：
		elif contentLine.startswith(">"):
			content += u"%s\n" %(contentLine)

		else:
			content += u"- %s\n" %(contentLine)

	return content


# ----- 检测参数
if "" == _version:
	__exit(u"请输入迭代号；如：1.13 迭代")
elif _isIOS and _isModeAppStore and not nat_version.isValid(_versionName):
	__exit(u"请通过 --vsn VersionStr 输入正确的版本号；如：--vsn 1.0.0")
# elif not nat_path.isDir(_projectDir):
# 	__exit(u"项目目录不存在")
# elif not nat_path.isDir(_MINI_HTTPS_DIR):
# 	__exit(u"mini 服务器的 https 目录不存在：%s" %(_MINI_HTTPS_DIR))


# ----- iOS 准备
if _isIOS:

	# 拉代码
	if not nat_path.isDir(_projectDir) and not nat_git.pull("master"):
		__exit(u"拉代码失败啦！")

	pwdPath = nat_path.getCwd()
	desktop = nat_path.getlastPath(nat_path.getlastPath(pwdPath))
	nat_path.changeCwd(desktop)
	__warn(u"进入桌面路径%s" %(desktop))
	ipaPath = "%s/IPAS" %(desktop)
	if not nat_path.isExist(ipaPath):
		__warn(u"IPAS文件夹不存在、需要clone")
		nat_git.clone(_IPA_FILE_SERVER)
	else:
		__warn(u"IPAS文件夹%s已经存在、直接pull" %(ipaPath))
		nat_cmd.cmd("cd %s" %(ipaPath))
		nat_path.changeCwd(ipaPath)
		nat_git.pull()

	__IPADIR = ipaPath
	nat_path.changeCwd(pwdPath)

	# # ----- 创建分支
	# if not nat_path.isDir(_projectDir) and not nat_path.mkDirRecursive(_projectDir):
	# 	__exit(u"创建分支目录失败：%s" %(_projectDir))

	# nat_path.changeCwd(_projectDir)
	# _projectDir = nat_path.join(_projectDir, _projectName)
	# if not nat_path.isDir(_projectDir) and not nat_git.clone(__PROJECT_GIT_URL):
	# 	__exit(u"克隆项目到本地失败")

	# # ----- 更新当前分支到最新代码
	# nat_path.changeCwd(_projectDir)


	# if "develop" == __GIT_BRANCH_MODE:
	# 	targetBranchName = "Develop"
	# elif "developer" == __GIT_BRANCH_MODE:
	# 	targetBranchName = __GIT_BRANCH_MODE
	# else:
	# 	targetBranchName = nat_string.trim("qa-%s" %(_version))

	# if nat_git.branch_isCurMaster():
	# 	nat_git.fetch()
	# 	if not nat_cmd.cmd("git checkout -b %s origin/%s" %(targetBranchName, targetBranchName)):
	# 		__exit(u"切换到目标分支 %s 失败，请先手动新增该分支到远程仓库" %(targetBranchName))

	# elif targetBranchName == nat_git.branch_current():
	# 	pass

	# else:
	# 	__exit(u"当前分支与目标分支不同，无法检出；当前分支：%s，目标分支：%s" %(nat_git.branch_current(), targetBranchName))

	# # 切换新分支
	# nat_git.pull(targetBranchName, enableEdit = False)
	# if _isIOS:
	# 	nat_git.push(targetBranchName)


# ----- 小帮 iOS 打包类
class XbIosArchive(nat_ipa._Archive):

	def _getDownloadIpaUrlDomain(self):
		return _DOWNLOAD_IPA_URL_DOMAIN

	# 如：/V1.1/01
	def _getDownloadIpaUrlPath(self):
		return "V%s/%s" %(_version, self.getCode())
		# return "/%s/%s/v%s/%s/%s/%s" %(self._projectName, _platform, _version, _mode, self.getCode(), self._getAvailExportedIpaDirBasename())

	def _getTeamID(self):
		return _TEAM_ID

	def getCode(self):
		codePath = self.getCodePath()
		if not nat_path.isFile(codePath):
			codeDirPath = os.path.dirname(codePath)
			if not nat_path.mkDirRecursive(codeDirPath) or not nat_file.createEmptyFile(codePath, "1"):
				nat_console.exit(u"创建 %s 文件失败" %(codePath))

		codeInt = int(nat_string.trim(nat_file.read(codePath)))
		return "%02d" %(codeInt)

	def getCodePath(self):
		exportDirPath = self._getAvailExportedDirPath()
		exportBasename = "export-%s/%s" %(_version, self._getIsReleaseModeStr().lower())
		exportDir = nat_path.join(exportDirPath, exportBasename)

		return nat_path.join(exportDir, _CODE_FILENAME)

	def _getExportedArchiveDirBasename(self):
		return "export-%s/%s/%s" %(_version, self._getIsReleaseModeStr().lower(), self.getCode())

	def _getProvisioningProfiles(self):
		extraList = []

		if not _isModeAppStore and None != _EXTRA_ADHOC_BUNDLEID_PROVISION_LIST:
			extraList = _EXTRA_ADHOC_BUNDLEID_PROVISION_LIST
		elif _isModeAppStore and None != _EXTRA_APPSTORE_BUNDLEID_PROVISION_LIST:
			extraList = _EXTRA_APPSTORE_BUNDLEID_PROVISION_LIST

		extraList.append((_BUNDLE_ID, _provisioningName))
		return extraList

	def _getSigningCertificate(self):
		return _certificateName

	def __mkReleaseTag(self):
		if self.__getIsStagingMode():
			return u"Staging"
		elif self._getIsReleaseMode():
			return u"正式"
		elif self._getIsAppStoreMode():
			return u"AppStore"
		else:
			return u"QA"


	def _getIsReleaseMode(self):
		return _isModeRelease

	def __getIsStagingMode(self):
		return _isModeStaging

	def _getIsAppStoreMode(self):
		return _isModeAppStore

	def _getIsReleaseModeStr(self):
		if self.__getIsStagingMode():
			return u"Staging"
		elif self._getIsReleaseMode():
			return u"Release"
		elif self._getIsAppStoreMode():
			return u"AppStore"
		else:
			return u"Debug"


	def _isForceEnableUpdateMd(self):
		return _isIOS and not _isModeAppStore

	def _isUpdateMdContentValid(self):
		content = self.getUpdateMdContent()
		return "" != content

	def _getUpdateContent(self):
		return self.getUpdateMdContent()


	def getUpdateMdContent(self):
		return "本次更新内容，可以在Jenkins上进行配置"
	# 	updateMdPath = self._getAvailUpdateMdPath()

	# 	content = ""
	# 	if nat_path.isFile(updateMdPath):
	# 		updateFileFp = open(updateMdPath, "r")
	# 		content += _parseUpdateLines(updateFileFp.readlines())
	# 		content += u"\n"
	# 		updateFileFp.close()


	# 	content += _parseUpdateLines(_releaseNote.split("\n"))
	# 	if "" == content:
	# 		return ""
	# 	else:
	# 		prefix = u"### 【提测-%s】\n#### 【iOS-%s迭代-%s】\n" %(self.__mkReleaseTag(), _version, self.getCode())
	# 		return prefix + content

	def _onErrorUpdateMdEmpty(self):
		cmdStr = "open -a Sublime\ Text %s" %(self._getAvailUpdateMdPath())
		nat_cmd.cmd(cmdStr)

	def _getUploadAppstoreApiKey(self):
		return _ITC_API_KEY

	def _getUploadAppstoreApiIssuer(self):
		return _ITC_ISSUER_ID

# ----- 实例化对象

archive = XbIosArchive(_projectDir, __projectname)

_code = archive.getCode()
__vsnCode = "v%s/%s" %(_version, _code)   # 如：v1.13/03
__vsnModeCode = "v%s/%s/%s" %(_version, _mode, _code)   # 如：v1.13/debug/03
__projectPlatformVsn = "%s/%s/v%s" %(archive._projectName, _platform, _version)   # 如：fq/ios/v1.13
__projectPlatformVsnMode = "%s/%s" %(__projectPlatformVsn, _mode)   # 如：fq/ios/v1.13/debug
__projectPlatformVsnModeCode = "%s/%s/%s" %(__projectPlatformVsn, _mode, _code)   # 如：fq/ios/v1.13/debug/03
__versionCode = "%s%s" %(nat_version.toCodeStr(_versionName), _code)   # 如：01010104
# __miniDirPath = "%s/%s" %(_MINI_HTTPS_DIR, __projectPlatformVsnModeCode)   # 如：/...web.../fq/ios/v1.13/release/03/
__manifestDownloadUrl = "%sV%s/%s/manifest.plist" %(_DOWNLOAD_IPA_URL_DOMAIN, _version, archive.getCode())
_scriptPath = "%s/script" %(archive._getAvailExportedDirPath())


# ----- iOS 打包
if _isIOS:
	# 进行 pod install
	nat_cmd.cmd("pod install")
	if __POD_UPDATE_FQ_RESOURCE:
		nat_cmd.cmd("pod update fq-resource")


	# 如果是上架 AppStore
	if _isModeAppStore:
		if not nat_path.isFile(__projectInfoPlistPath):
			__exit(u"项目的 Info.plist 文件不存在")

		nat_plist.setStr(__projectInfoPlistPath, "CFBundleShortVersionString", _versionName)
		nat_plist.setStr(__projectInfoPlistPath, "CFBundleVersion", "%s" %(__versionCode))

		if None != _EXTRA_APPSTORE_BUNDLEID_PROVISION_LIST:
			for (bundleId, _) in _EXTRA_APPSTORE_BUNDLEID_PROVISION_LIST:
				targetName = bundleId.split(".")[-1]
				extraProjectInfoPlistPath = "%s/%s/Info.plist" %(_projectDir, targetName)
				if nat_path.isFile(extraProjectInfoPlistPath):
					nat_plist.setStr(extraProjectInfoPlistPath, "CFBundleShortVersionString", _versionName)
					nat_plist.setStr(extraProjectInfoPlistPath, "CFBundleVersion", "%s" %(__versionCode))

		nat_git.addAndCommit(u"【开始上传】%s" %(_versionName))

	else:
		# 读取版本更新内容
		updateMdPath = archive._getAvailUpdateMdPath()
		updateMdContent = archive.getUpdateMdContent()

		# 添加 tag
		nat_git.tag_add(__vsnModeCode)
		nat_git.addAndCommit(u"【开始提测】%s" %(__vsnModeCode))

		# 清空 update.md
		nat_cmd.cmd("echo \"\" > %s" %(updateMdPath))
		nat_git.addAndCommit(u"【完成提测】")

	# # 修改ipa输出路径 同一个版本号每次自增
	# codePath = archive.getCodePath()
	# codeDirPath = os.path.dirname(codePath)
	# code = "%s" %(int(archive.getCode()) + 1)
	# nat_file.write(codeDirPath, _CODE_FILENAME, code)

	# ----- 打包操作
	if not _isFastlane:
		__warn(u"开始清理项目")
		if not archive.doClean():
			__exit(u"清理项目失败")
			__warn(u"开始生成 archive")
		if not archive.doGenArchive():
			__exit(u"生成 archive 失败")

		# ----- 上传到 appstore
		if _isModeAppStore:
			__warn(u"开始导出 IPA")
			if not archive.doExportIpaForAppStore():
				__exit(u"导出 IPA 失败")

				__warn(u"开始上传到 appstore")
			if not archive.doUploadToAppStore():
				__exit(u"上传到 appstore 失败")

		# ----- AdHoc 打包
		else:
			__warn(u"开始导出 IPA")
			if not archive.doExportIpaForAdHoc():
				__exit(u"导出 IPA 失败")


			# ----- 推送 manifest 到 gitee
			__warn(u"开始提交 manifest.plist 到远程仓库")
			# archiveExportDirPath = archive._getAvailExportedArchiveDirPath()
			# nat_path.changeCwd(archiveExportDirPath)
			# nat_git.clone(__DEFAULT_GITEE)

			# # 跳转到 gitee 目录
			# giteeBasename = os.path.splitext(os.path.basename(__DEFAULT_GITEE))[0]   # xb
			# giteeDirPath = os.path.join(archiveExportDirPath, giteeBasename)
			# nat_path.changeCwd(giteeDirPath)

			# # 创建迭代目录
			# manifestDirPath = "%s/%s" %(giteeDirPath, __projectPlatformVsnMode)
			# nat_path.mkDirRecursive(manifestDirPath)

			# 拷贝 manifest.plist 文件
			# manifestPlistNewPath = nat_path.join(manifestDirPath, "%s.plist" %(_code))
			# manifestPlistPath = archive._getAvailExportedIpaManifestPlistPath()
			# nat_plist.remove(manifestPlistPath, ":items:0:assets:2")   # 移除 full-size-image
			# nat_plist.remove(manifestPlistPath, ":items:0:assets:1")   # 移除 display-image
			# nat_cmd.cmd("cp %s %s" %(manifestPlistPath, manifestPlistNewPath))

			# 加入版本控制
			# nat_git.add()
			# if not nat_git.status_isClean():
			# 	nat_git.commit(u"[Auto Commit]")

			# nat_git.push()
			# __warn(u"已推送 manifest.plist")

			# # ----- 同步数据到 mini 的 https 地址
			# __warn(u"开始同步资料到 mini")
			# nat_path.rmDirRecursive(__miniDirPath)
			# nat_path.mkDirRecursive(__miniDirPath)
			

			# 同步 更新文件
			# updateMdFilePath = nat_path.join(__miniDirPath, "update.md")
			# if not nat_file.write(__miniDirPath, "update.md", updateMdContent):
			# 	__exit(u"推送 更新文件 到 mini 失败")
	else:
		manifestPath = "%s/%s/manifest.plist" %(archive._getAvailExportedDirPath(), archive._getExportedArchiveDirBasename())
		appURL = "%s%s/%s.ipa" %(archive._getDownloadIpaUrlDomain(), archive._getDownloadIpaUrlPath(), _projectName)
		if fastlaneDoArchive(appURL):
			__exit(u"fastlane 打包失败，赶紧看看")

	# 同步 ipa
	currentCode = archive.getCode()
	ipaDirPath = archive._getAvailExportedIpaDirPath()
	if not nat_path.cpDir(ipaDirPath, "%s/V%s/%s" %(__IPADIR, _version, currentCode)):
		__exit(u"同步ipa到 IPAS文件中 失败")

	# 检测index.html文件是否存在
	indexPath = "%s/index.html" %(__IPADIR)
	if not nat_path.isExist(indexPath):
		# index.html文件不存在 拷贝script里面的html
		nat_path.changeCwd(_scriptPath)
		originIndexPath = "%s/index.html" %(_scriptPath)
		__warn(u"将 %s 拷贝到 %s" %(originIndexPath, indexPath))
		if nat_cmd.cmd("cp -a -f %s %s" %(originIndexPath, indexPath)):
			__warn(u"拷贝index.html文件成功啦")
		else:
			__exit(u"拷贝index.html失败了，兄嘚！赶紧看看咋回事儿吧")
		
	# 切换工作路径
	nat_path.changeCwd(__IPADIR)
	# 创建a标签
	# <a title="iPhone" href="itms-services://?action=download-manifest&url=https://gitee.com/caimter/IPAS/raw/master/V1.1/05/ipa/manifest.plist">debug_V1.1_05</a>			
	downloadManifest = "itms-services://?action=download-manifest&url=%s" %(__manifestDownloadUrl)
	manifestName = "%s_V%s_%s" %(archive._getIsReleaseModeStr().lower(), _version, currentCode)

	soup = BeautifulSoup(open("index.html"))
	br = soup.new_tag("br")
	soup.div.append(br)

	new_tag = soup.new_tag("a", href = "%s" %(downloadManifest))
	new_tag.string = manifestName
	soup.div.append(new_tag)

	html = soup.prettify("utf-8") 
	with open("index.html", "wb") as file:
		file.write(html)

	# 版本控制
	nat_git.add()
	if not nat_git.status_isClean():
		nat_git.commit(u"[Auto Commit]")
	nat_git.push()
	downloadUrl = "http://htmlpreview.github.io/?https://gitee.com/caimter/IPAS/raw/master/index.html"
	__warn(u"-----------------------------------------")
	__warn(u"%s" %(downloadUrl))
	__warn(u"-----------------------------------------")



# ----- 通知企业微信群
if not _isSilence:
	__warn(u"开始通知企业微信群")

	# iOS
	if _isIOS:
		if _isModeAppStore:
			# md
			msgMd = nat_group_robot.MarkdownMessage(__QIYE_WECHAT_WEBHOOK)
			updateMdContent = u"### 【准备提审-AppStore】\n#### 【iOS-%s迭代-%s】\n" %(_version, archive.getCode())
			msgMd.append(updateMdContent)

			# at 所有人
			msgText = nat_group_robot.TextMessage(__QIYE_WECHAT_WEBHOOK)
			msgText.setContent(u"版本：%s\n版本编号：%s" %(_versionName, __versionCode))
			msgText.setMentionedAll()

			# 发送
			msgMd.send()
			msgText.send()

		else:
			# md
			# msgMd = nat_group_robot.MarkdownMessage(__QIYE_WECHAT_WEBHOOK)
			# msgMd.append(updateMdContent)

			# at 所有人
			# standbyUrl = "http://%s/download/?n=%s&p=%s&v=%s&m=%s&c=%s" %(__SERVER_HOST_IP, _projectName, _platform, _version, _mode, _code)
			msgText = nat_group_robot.TextMessage(__QIYE_WECHAT_WEBHOOK)
			msgText.setMentionedAll()
			msgText.setContent(u"App名称:%s 版本号:%s_%s\n下载地址:：%s\n备注:\n%s" %(_projectName, _version, archive.getCode(), downloadUrl, updateMdContent))

			# 二维码：原始链接
			qrGenerator = nat_qrcode.Generator()
			qrGenerator.setDestDirPath(archive._getAvailExportedArchiveDirPath())
			qrGenerator.setDestBasename(archive._projectName)
			qrGenerator.setUrl(__manifestDownloadUrl, True)
			# qrGenerator.setBgImgPath(__QRCODE_BG_PNG_PATH)
			if not qrGenerator.generate():
				__exit(u"生成下载二维码失败")

			qrPath = qrGenerator.getQrPath()
			img = nat_image.NatImage(qrPath)
			img.setWidth(150, True)

			# 中间添加logo
			if nat_path.isFile(__APP_LOGO_FILE):
				logoImg = nat_image.NatImage(__APP_LOGO_FILE)
				logoImg.setRGBA().setWidth(50, True)

				img.setRGBA()
				img.pasteImg(logoImg, 50, 50)

			img.save()

			msgImg = nat_group_robot.ImageMessage(__QIYE_WECHAT_WEBHOOK)
			msgImg.setImg(qrPath)

			# 二维码：备用链接
			# qrGeneratorStandby = nat_qrcode.Generator()
			# qrGeneratorStandby.setDestDirPath(archive._getAvailExportedArchiveDirPath())
			# qrGeneratorStandby.setDestBasename("%s_standby" %(archive._projectName))
			# qrGeneratorStandby.setUrl(standbyUrl)
			# qrGeneratorStandby.setBgImgPath(__QRCODE_BG_STANDBY_PNG_PATH)
			# if not qrGeneratorStandby.generate():
			# 	__exit(u"生成备用二维码失败")
			#
			# qrPathStandby = qrGeneratorStandby.getQrPath()
			# imgStandby = nat_image.NatImage(qrPathStandby)
			# imgStandby.setWidth(150, True)
			# imgStandby.save()
			#
			# msgImgStandby = nat_group_robot.ImageMessage(__QIYE_WECHAT_WEBHOOK)
			# msgImgStandby.setImg(qrPathStandby)

			# 同步二维码图片到 mini
			# nat_cmd.cmd("cp %s %s/" %(qrPath, __miniDirPath))
			# nat_cmd.cmd("cp %s %s/" %(qrPathStandby, __miniDirPath))

			# 发送消息
			# msgMd.send()
			msgText.send()
			msgImg.send()
			# msgImgStandby.send()


# 推送到 qa 分支
if _isIOS:
	nat_path.changeCwd(_projectDir)
	nat_git.pull(enableEdit = False)
	if not nat_git.push(withTags = True):
		__exit(u"推送分支 %s 失败" %(targetBranchName))



# 推送到自动化建设 - app 包管理
# __warn(u"开始推送到集成平台")
# __QUALITY_QA_URL = "https://quality-qa.xiaobangtouzi.com/treasure/api/v1/create"
# __QUALITY_QA_HEADER = "Content-Type:application/json"
# __QUALITY_QA_DATA = """{"name": "%s","version_name": "%s", "version": "%s", "branch": "%s", "platform": "%s", "commit_id": "%s", "type": "%s", "submitter": "%s", "created": "%s", "url": "%s"}"""


# if _isIOS:
# 	nat_path.changeCwd(_projectDir)
# 	gitLog = os.popen("git log -1")
# 	commit_id = gitLog.readline().split(" ")[1].strip()
# 	submitter = gitLog.readline().split(" ")[1].strip()
# 	quality_qa_download_url = "itms-services://?action=download-manifest&url=%s" %(__manifestDownloadUrl)
# else:
# 	commit_id = archive._argCommitId
# 	submitter = archive._argCommitName
# 	targetBranchName = archive._argCommitBranch
# 	quality_qa_download_url = archive._mkApkDownloadUrl()

# quality_qa_data = __QUALITY_QA_DATA %(__APP_NAME, _version, _code, targetBranchName, _platform, commit_id, _mode, submitter, datetime.datetime.now(), quality_qa_download_url)
# quality_qa_requrest = "curl '%s' -H '%s' -d '%s'" %(__QUALITY_QA_URL, __QUALITY_QA_HEADER, quality_qa_data)
# nat_cmd.cmd(quality_qa_requrest)


# 开始进行清理工作
__warn(u"开始清理工作")
if _isIOS:
	# 删除 *.xcarchive 和 提审 appstore 产生的 ipa
	archive.removeExportedArchive()
	archive.removeExportedIap()

else:
	pass



# ----- 递增迭代 code 号
_code = int(_code) + 1
nat_cmd.cmd("echo %d > %s" %(_code, archive.getCodePath()))



# ----- 完成打包
__costSeconds = __timestamp() - __scriptStartTimestamp
__costMinutes = __costSeconds / 60
__costSeconds = __costSeconds % 60
__warn(u"完成打包任务，共耗时：%i 分 %i 秒" %(__costMinutes, __costSeconds))


















